<?php
/*
 * code to display the grid of drop down lists to allow for
 *  the management of timesheet periods by admin staff
*/
// Create connection
include("../inc_files/utils/dbconnection.php");

$query ="SELECT * FROM timesheet_periods WHERE LEFT(PeriodID,4) = '" . $_GET["year"] . "' ORDER BY PeriodID ASC";
$result = $mysqli->query( $query );

//count the number of returned results
$resultCount = $result->num_rows;

//if there are some results returned create a table showing the results
if( $resultCount > 0){
	$rownumber = 0;

	echo "<table class='datagrid'>
			<tr class=\"datagrid headerrow\">

			<th colspan=\"10\" style=\"width:100%\">
			Timesheet periods for " . $_GET["year"]."
					</th>



					</tr>
					<tr class=\"datagrid headerrow\">

					<th style=\"width:10%\">ID</th>
					<th style=\"width:10%\">Start Date</th>
					<th style=\"width:10%\">Open</th>
					<th style=\"width:10%\">Sun</th>
					<th style=\"width:10%\">Mon</th>
					<th style=\"width:10%\">Tue</th>
					<th style=\"width:10%\">Wed</th>
					<th style=\"width:10%\">Thur</th>
					<th style=\"width:10%\">Fri</th>
					<th style=\"width:10%\">Sat</th>


					</tr>";

	while($row = mysqli_fetch_array($result))
	{

		if($rownumber %2 == 0)
		{
			echo "<tr class=\"datagrid evenrow\">";
		}
		else
		{
			echo "<tr class=\"datagrid oddrow\">";
		}
			
		echo "<td style=\"width:10%\">". $row['PeriodID'] ."</td>";
		echo "<td style=\"width:10%\">".date('d/m/y',strtotime($row['StartDate']))."</td>";
		echo "<td style=\"width:10%\"  class=\"week{$row['Closed']}Cell\">". createWeekDLL($row['Closed'], $row['PeriodID'].'Closed') ."</td>";
		echo "<td style=\"width:10%\"  class=\"day{$row['SunStatus']}Cell\">" . createDayDDL($row['SunStatus'],$row['PeriodID'].'SunStatus') ."</td>";
		echo "<td style=\"width:10%\"  class=\"day{$row['MonStatus']}Cell\">" . createDayDDL($row['MonStatus'],$row['PeriodID'].'MonStatus') . "</td>";
		echo "<td style=\"width:10%\"  class=\"day{$row['TueStatus']}Cell\">" . createDayDDL($row['TueStatus'],$row['PeriodID'].'TueStatus') ."</td>";
		echo "<td style=\"width:10%\"  class=\"day{$row['WedStatus']}Cell\">" . createDayDDL($row['WedStatus'],$row['PeriodID'].'WedStatus') ."</td>";
		echo "<td style=\"width:10%\"  class=\"day{$row['ThurStatus']}Cell\">" . createDayDDL($row['ThurStatus'],$row['PeriodID'].'ThurStatus') ."</td>";
		echo "<td style=\"width:10%\"  class=\"day{$row['FriStatus']}Cell\">" . createDayDDL($row['FriStatus'],$row['PeriodID'].'FriStatus') ."</td>";
		echo "<td style=\"width:10%\"  class=\"day{$row['SatStatus']}Cell\">" . createDayDDL($row['SatStatus'],$row['PeriodID'].'SatStatus') ."</td>";


			
		echo "</tr>";
		$rownumber++;
	}
	echo "</table>";


}

//if no results returned say so.
else
{

	echo"No records returned";
}
//tidy up database connection
$mysqli->close();


function createWeekDLL($weekIsClosed, $periodID){
	$returnDDL = "<select id='_#".$periodID."' name='".$periodID."' class='week' onchange='changeWeekBackground(this)'>";
	if($weekIsClosed == 0){
		$returnDDL .= "<option value='0'  class='open' selected>Open</option>
				<option value='1'  class='closed'>Closed</option>
				</select>";
	} else {
		$returnDDL .= "<option value='0'  >Open</option>
				<option value='1'   selected>Closed</option>
				</select>";
	}

	return $returnDDL;
}

//create a status drop down list for each day
function createDayDDL($dayStatus, $dayID){

	$returnDDL = "";



	// if the option for the day is blocked
	// e.g. it is the last day of the previous
	// year which is in the first week of
	// the following year disable the DDL
	if($dayStatus == 1)
	{
		$returnDDL .= "<select style='visibility:hidden;' name='".$dayID."' id='".$dayID."' disabled>
				<option value='1' >Block</option>
				</select>";
	} else if ($dayStatus == 2){
		//if the day isn't blocked delete this option{
		$returnDDL .= "<select name='".$dayID."' class='day' id='".$dayID."' onchange='changeDayBackground(this)'>
		<option value='2' class='working' selected>Work</option>
		<option value='3' class='nonworking'>N/W</option>
		</select>";
	}else {
		//if the day isn't blocked delete this option{
		$returnDDL .= "<select name='".$dayID."' class='day' id='".$dayID."' onchange='changeDayBackground(this)'>
		<option value='2' class='working'>Work</option>
		<option value='3' class='nonworking' selected>N/W</option>
		</select>";
	}


	return $returnDDL;
}


?>
